package com.mzxy.c1625163415.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mzxy.c1625163415.R;
import com.mzxy.c1625163415.model.Stories;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class NewestRecyclerViewAdapter extends
        RecyclerView.Adapter implements View.OnClickListener {
    private Context mContext;
    private LayoutInflater inflater;
    private OnItemClickListener mOnItemClickListener = null;
    private List<Stories> datas = new ArrayList<>();
    private ViewHolder viewholder;
    private Activity activity;

    public NewestRecyclerViewAdapter(Context context, Activity activity) {
        this.mContext = context;
        this.activity = activity;
        inflater = LayoutInflater.from(mContext);
    }

    /**
     * @Description: RecyclerView item 点击事件
     * @Author: lixh
     * @CreateDate: 2019/5/5 15:42
     * @Version: 1.0
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.recyclerview_item, null, false);
        viewholder = new ViewHolder(view);
        view.setOnClickListener(this);
        return viewholder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof ViewHolder) {
            ViewHolder holder = (ViewHolder) viewHolder;
            Stories model = datas.get(position);
            Glide.with(mContext).load(model.getImages().get(0)).into(viewholder.iv_image);
            holder.tv_detail.setText(model.getTitle());
            viewholder.itemView.setTag(position);
        }
    }

    public void clearAdapter(boolean isclear) {
        if (isclear)
            datas.clear();
    }

    public void refresh(List<Stories> collection, boolean clear) {
        // 之前刷新是否先清理数据
        if (clear) {
            datas.clear();
        }
        if (!clear) {
            Iterator<Stories> it = collection.iterator();
            while (it.hasNext()) {
                Stories obj = it.next();
                if (datas.contains(obj)) {
                    it.remove();
                }
            }
        }
        datas.addAll(0, collection);
        notifyDataSetChanged();
    }

    public void loadMore(List<Stories> collection, String mark) {
        // 操作list的增删需要 使用迭代器
        Iterator<Stories> it = collection.iterator();
        while (it.hasNext()) {
            Stories obj = it.next();
            if (datas.contains(obj))
                it.remove();
        }
        datas.addAll(collection);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public void onClick(View view) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(view, (int) view.getTag());
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public TextView tv_detail;

        public ImageView iv_image;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_detail = itemView.findViewById(R.id.tv_detail);
            iv_image = itemView.findViewById(R.id.iv_image);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
}
